#include "cmdapi.h"
#include "../mainctrl.h"

char CCmdApi::szBuf[CTMsgHead::EMAX_MSGLEN];

CT_REGISTER_ASYNCCMD(ID_MAINAPI_TAPI , CCmdApi);

CCmdApi::CCmdApi() : iSendTime(0) {}

CCmdApi::~CCmdApi() {}

int CCmdApi::Do(void* pData)
{
    if(pstMainCtrl == NULL )
    {
        CT_WARNING((""));
        return Done(RET_SYSTEM_FAIL);
    }

    CallBack(RET_OK);
    return RET_OK;
}

int CCmdApi::CallBack(int iRet)
{
    if (iRet)
    {
        CT_ERROR(("%d", iRet));
        return Done(iRet);
    }

    if (pstMainCtrl->m_iNow < iSendTime)
    {
        PushQueue(iRet); 
        return iRet;
    }
    
    CTMsgHead stMsgHead;
    stMsgHead.shMsgID = ID_MAINAPI_TAPI ;
    stMsgHead.shMsgType = EMT_REQUEST;
    stMsgHead.iCmdSeq = GetCmdSeq();
    stMsgHead.llMsgAct = reinterpret_cast<int64>(this);

    CTBuffer& stBuf = pstMainCtrl->m_stEncodeBuf;
    CTCodeStream s(stBuf);

    CodeConvert(s, stMsgHead, NULL, bin_encode());
    CodeConvert(s, stReq, NULL, bin_encode());
    if (s.m_iRet)
    {
        CT_WARNING(("Fail to encode request, ret = %d", s.m_iRet));
        return Done(RET_SYSTEM_FAIL);
    }

    iRet = CMainCtrl::m_pstMain->RequestToApi(s.m_pszBuf, s.m_ushPos);
    if (iRet)
    {
        CT_WARNING(("Fail to send request to server, ret = %d", iRet));
        return Done(RET_SYSTEM_FAIL);
    }

    return iRet;
}

int CCmdApi::OnAnswer(void * pData)
{
    int iRet = 0;

    if (NULL == pData)
    {
        CT_ERROR(("NULL input pointer"));
        return Done(RET_INVALID_PARAM);
    }

    CCmdOnAnswerParam* pstParam = (CCmdOnAnswerParam*) pData;
    if((CMainCtrl*)(pstParam->m_pMain) != pstMainCtrl ||
        pstParam->m_pstMsgHead->shMsgID != ID_MAINAPI_TAPI ||
        pstParam->m_pstMsgHead->shMsgType != EMT_ANSWER)
    {
        return Done(RET_SYSTEM_FAIL);
    }

    //解包
    iRet = CodeConvert(*pstParam->m_pstCode, stAns, NULL, bin_decode());
    if (iRet != 0)
    {
        CT_WARNING(("decode ans error"));
        return Done(iRet);
    }

    if (stAns._stResult.iResultID)
    {
        return Done(stAns._stResult.iResultID);
    }
    
    if (strlen(stAns._stTApiKeyValue.GetValue("ret")) <= 0)
    {
        CT_ERROR((""));
        return Done(RET_SYS_ERROR);
    }
    
    //错误码转换
    iRet = atoi(stAns._stTApiKeyValue.GetValue("ret"));

    switch (iRet)
    {
    case 0:
        iRet = RET_OK;
        break;
    case 1300:
    case 1002:
        iRet = RET_NOT_LOGIN;
        break;
    case 2:
        iRet = ERROR_TAPI_RET_1;
        break;
    case 1003:
        iRet = ERROR_TAPI_RET_2;
        break;
    case 1004:
        iRet = ERROR_TAPI_RET_3;
        break;
    case 1005:
        iRet = ERROR_TAPI_RET_4;
        break;
    case 1006:
        iRet = ERROR_TAPI_RET_5;
        break;
    case 1100:
        iRet = ERROR_TAPI_RET_6;
        break;
    case 1101:
        iRet = ERROR_TAPI_RET_7;
        break;
    case 1102:
        iRet = ERROR_TAPI_RET_8;
        break;
    case -6:
        iRet = ERROR_TAPI_RET_9;
        break;
    case 1062: //通知过早订单尚未挂起，请稍候再试(可等待几秒后，重复通知)。 
        {
            iSendTime = pstMainCtrl->m_iNow + 2;
            PushQueue(RET_OK);
            return RET_OK;
        }
        break;
    default:
        iRet = RET_SYS_ERROR;
        break;
    }

    //为了防止任务集市出错,增加一个特殊处理
    if ((iRet != RET_OK) && (stReq._szTApiName == "/v3/pay/check_task_status"))
    {
        CTLib::CTKeyValue & stTApiKeyValue = stAns._stTApiKeyValue;
        for (int i = 0; i < stTApiKeyValue._astKeyValueData.Size(); ++i)
        {
            CT_ERROR(("%s=%s", stTApiKeyValue._astKeyValueData[i]._szKey(), stTApiKeyValue.GetValue(stTApiKeyValue._astKeyValueData[i]._szKey())));
        }
        
        iRet = RET_OK;
    }
    
    return Done(iRet);
}


